package tech.lithum.mm.ui.home;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import tech.lithum.mm.api.LoginRequest;
import tech.lithum.mm.api.LoginResponse;

public class HomeViewModel extends ViewModel {

    private final MutableLiveData<String> mText;

    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public HomeViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("This is home fragment");

        new Thread(new Runnable() {
            @Override
            public void run() {
                String token;
                LoginRequest loginRequest = new LoginRequest();
                loginRequest.setUsername("admin");
                loginRequest.setPassword("27a3f7b3406acd7eebecc2843a9c4edb76d5108676b2b4af296a63bb46d64b1ebbc3e69269c8169dbb0fc40a35747626503bffdba94245e5462d9461cda08dec");
                OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = RequestBody.create(new Gson().toJson(loginRequest), JSON);
                Request request = new Request.Builder().url("http://139.224.62.22:7777/api/login").post(requestBody).build();
                Response response = null;
                try {
                    response = client.newCall(request).execute();
//                    Log.i("mm", response.body().string());

                    LoginResponse loginResponse = new Gson().fromJson(response.body().string(), LoginResponse.class);
                    token = loginResponse.getToken();
//                    mText.postValue(response.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

//                RunWorkflowRequest runWorkflowRequest = new RunWorkflowRequest();
//                runWorkflowRequest.setSync(true);
//                runWorkflowRequest.setName("微博");
//                requestBody = RequestBody.create(new Gson().toJson(runWorkflowRequest), JSON);
//                request = new Request.Builder().url("http://139.224.62.22:7777/api/run")
//                        .post(requestBody)
//                        .addHeader("Authorization", "Bearer " + token)
//                        .build();
//                try {
//                    response = client.newCall(request).execute();
//                    RunWorkflowResponse runWorkflowResponse = new Gson().fromJson(response.body().string(), RunWorkflowResponse.class);
//                    JsonObject obj = new Gson().fromJson(runWorkflowResponse.getData(), JsonObject.class);
//                    JsonArray array = obj.getAsJsonArray("get_weibo_data");
//                    for(int i = 0 ; i < array.size() ; i++) {
//                        JsonElement element = array.get(i);
//                        System.out.println(element.getAsJsonObject());
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }

            }
        }).start();
    }

    public LiveData<String> getText() {
        return mText;
    }
}